<template>
  <div class="app-container">
      <FilterForm :data="filterData" @search="getList" labelWidth="86px" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:woodBasicsData:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:woodBasicsData:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:woodBasicsData:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <Table
      :api="$Api.port.list"
      :params="{basicsType:5}"
      :selection="true"
      @select="handleSelectionChange"
      ref="Table"
      height="60vh">
      <el-table-column label="名称" align="center" prop="basicsName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" key="basicsStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.basicsStatus"
            active-value = "0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
         </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['business:woodBasicsData:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['business:woodBasicsData:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </Table>

    <!-- 添加或修改木业基础数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="basicsName">
          <el-input v-model="form.basicsName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.basicsStatus">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{dict.dictLabel}}
              </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "WoodBasicsData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
         // 状态数据字典
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        baseCode: null,
        basicsName: null,
        days: null,
        baseCurrency: null,
        basicsStatus: null,
        basicsType: '5',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        basicsName:[
          {required: true, message: '请输入港口名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      },
      filterData:[
        {
          label: "名称",
          name: "basicsName",
          type: "none",
        },
        {
          label: "状态",
          name: "basicsStatus",
          type: "select",
          select: {
            option: [],
            selectLabel: "dictLabel",
            selectValue: "dictValue",
            multiple: true,
          },
        },
      ]
    };
  },
  created() {

    this.getDicts("sys_normal_disable").then(res => {
      let { data } = res;
        this.statusOptions = data;
        this.filterData.forEach( (v) =>{
          if( v.name == "basicsStatus") {
              v.select.option = data;
          }
        })
    });
  },
  methods: {
    /** 查询港口数据列表 */
    getList(params) {
      this.$refs.Table.getData(params);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        basicsName: null,
        basicsStatus: "0",
        remark: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.basicsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加木业基础数据";
    },
    // 修改港口状态
    handleStatusChange(row){
      let text = row.basicsStatus === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.basicsName + '"用户吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      })
      .then( ()=>{
            this.$Api.port.update(row)
                .then( (res)=>{
                    let { code } = res;
                    if( code == 200 ) {
                        this.getList();
                        this.msgSuccess(text + "成功");
                    }
                })
        }).catch( ()=>{
              row.basicsStatus = row.basicsStatus === "0" ? "1" : "0";
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const basicsId = row.basicsId || this.ids
      this.$Api.port.detail(basicsId).then(res=> {
        this.form = res.data;
        this.open = true;
        this.title = "修改港口信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.basicsId != null) {
             this.$Api.port.update(this.form).then(res=> {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.basicsType  = this.queryParams.basicsType
             this.$Api.port.add(this.form).then(res => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const basicsIds = row.basicsId || this.ids;
      this.$confirm('是否确认删除木业基础数据编号为"' + basicsIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then( ()=>{
            this.$Api.port.del(basicsIds)
                .then( (res)=>{
                    let { code } = res;
                    if( code == 200 ) {
                        this.getList();
                        this.msgSuccess("删除成功");
                    }
                })
        }).catch( ()=>{})
        
    },
  }
};
</script>
